#include "stdafx.h"
#include "VisualStudioApp.h"
#include "MainCommand.h"
#include "VisualStudioView.h"
#include "VisualStudio_i.c"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CVisualStudioApp
class CVisualStudioModule :
	public CAtlMfcModule
{
public:
	DECLARE_LIBID(LIBID_VisualStudioLib)
	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_VISUALSTUDIO, "{355EAB50-1155-4838-8445-5A43572F9DAE}")
};

CVisualStudioModule _AtlModule;

BEGIN_MESSAGE_MAP(CVisualStudioApp, CWinApp)
END_MESSAGE_MAP()

// CVisualStudioApp construction

CVisualStudioApp::CVisualStudioApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

// The one and only CVisualStudioApp object
CVisualStudioApp theApp;

// CVisualStudioApp initialization
BOOL CVisualStudioApp::InitInstance()
{
	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);
	CoInitialize(NULL);
	// Parse command line for standard shell commands, DDE, file open

	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
#if !defined(_WIN32_WCE) || defined(_CE_DCOM)
	// Register class factories via CoRegisterClassObject().
	if (FAILED(_AtlModule.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE)))
		return FALSE;
#endif // !defined(_WIN32_WCE) || defined(_CE_DCOM)
	// App was launched with /Embedding or /Automation switch.
	// Run app as automation server.
	if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
	{
		// Don't show the main window
		return TRUE;
	}
	// App was launched with /Unregserver or /Unregister switch.
	if (cmdInfo.m_nShellCommand == CCommandLineInfo::AppUnregister)
	{
		_AtlModule.UpdateRegistryAppId(FALSE);
		_AtlModule.UnregisterServer(TRUE);
		return FALSE;
	}
	// App was launched with /Register or /Regserver switch.
	if (cmdInfo.m_nShellCommand == CCommandLineInfo::AppRegister)
	{
		_AtlModule.UpdateRegistryAppId(TRUE);
		_AtlModule.RegisterServer(TRUE);
		return FALSE;
	}

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need
	// Change the registry key under which our settings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	HRESULT hr = E_FAIL;

	SAF_TRY;

	hr = safBaseClass::InitComFramework(safBaseClass::NotLoadLanguage);
	
	if (S_OK != hr)
		throw saf::CComException(hr);

	hr = this->DispEventAdvise((IUnknown*)this->GetComFramework());
	if (S_OK != hr)
		throw saf::CComException(hr);

	hr = CreateMDISplitterWnd(SW_SHOW);
	if (S_OK != hr)
		throw saf::CComException(hr);
	hr = GetComFramework()->Run();

	hr = this->DispEventUnadvise((IUnknown*)this->GetComFramework());
	if (S_OK != hr)
		throw saf::CComException(hr);

	::CoUninitialize();

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return TRUE;

	SAF_CATCH;

	return FALSE;
}


//Application quit.
void __stdcall CVisualStudioApp::OnQuit(VARIANT_BOOL* bExit)
{

}


void __stdcall CVisualStudioApp::OnCloseMainFrame(BSTR strKind, saf::IMainFrame* pMainFrame, VARIANT_BOOL *bExit)
{
	CComBSTR	strTitle;

	strTitle.LoadString(IDR_MAINFRAME);

	if (StrCmpI(strTitle, strKind) == 0)
	{
		this->m_pMainFrame.Release();
	}
}


HRESULT CVisualStudioApp::CreateMDISplitterWnd(int nShowCmd)
{
	CComBSTR	strTitle;
	HRESULT		hr		= S_FALSE;
	HICON		hIcon	= (HICON)::LoadImage(SafGetInstanceHandle(),
		MAKEINTRESOURCE(IDR_MAINFRAME),	IMAGE_ICON,	16,	16,
		LR_DEFAULTCOLOR);
	HMENU		hMenu	= ::LoadMenu(SafGetResourceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));

	strTitle.LoadString(IDR_MAINFRAME);
	hr = this->GetComFramework()->CreateMainFrame(saf::safMainFrameTypeMDISplitter,
		hMenu, (LONG_PTR)hIcon, strTitle, &m_pMainFrame);
	if (S_OK == hr)
	{
		CMainCommand	*pMainCommand = new CMainCommand();
		this->m_pMainFrame->put_NotifyCommand((saf::INotifyCommand*)pMainCommand);  
		this->GetComFramework()->put_ApplicationName(strTitle);
		this->m_pMainFrame->LoadAccelerators((LONG_PTR*)SafGetInstanceHandle(), IDR_MAINFRAME);

		this->CreateTools();
		this->CreateStatusBar();

		CComPtr<saf::ISplitterWnd>	pSplitterWnd;
		CComPtr<saf::ISplitterCell> pMainCell;
		CComPtr<saf::ISplitterCell> pLeftCell;
		CComPtr<saf::ISplitterCell> pRightCell;
		CComPtr<saf::IGenericPanel> pLeftGenericPanel;
		CComPtr<saf::IGenericPanel> pRightGenericPanel;

		this->m_pMainFrame->get_SplitterWnd(&pSplitterWnd);
		pSplitterWnd->CreateSplitterItem(saf::safSplitterWndTypeVertical, 250,
											&pLeftCell, &pRightCell, &pLeftGenericPanel, &pRightGenericPanel);

		// Left Cell.
		CVisualStudioView *pLeftView = NULL;
		pLeftView = new CVisualStudioView();
		pLeftCell->put_Name(L"TITLE_LEFTCELL");

		pLeftGenericPanel->InsertItem(0, TITLE_LEFTCELL, LoadIcon(IDR_MAINFRAME),
										(saf::IDataView*)pLeftView, (LONG_PTR*)pLeftView, NULL);

		//Move IMDIContainer to right cell.
		CComPtr<saf::IMdiContainer>		pMDIContainer;
		CComPtr<saf::IDataView>			pDataView;
		this->m_pMainFrame->get_MdiContainer(&pMDIContainer);
		pMDIContainer->QueryInterface(saf::IID_IDataView, (void**)&pDataView);
		pRightCell->put_Name(L"MdiContainer");
		pRightGenericPanel->InsertItem(0, L"MdiContainer", 0, (saf::IDataView*)pDataView, (LONG_PTR*)pDataView.p, NULL);

		////pSplitterWnd->UpdateLayout();

		CComPtr<saf::IAddIns> pIns;
		this->GetComFramework()->get_AddIns((IDispatch**)&pIns);
		pIns->LoadAddIn(L"MSDNBody.CombinationBody", L"MSDNBody", VARIANT_TRUE);

		//		pSplitterWnd->UpdateLayout();

		this->m_pMainFrame->ShowWindow(nShowCmd);
	}

	return hr;
}


void CVisualStudioApp::CreateTools()
{
	ATLASSERT(NULL != m_pMainFrame);

	CComPtr<saf::ICommandBars>	pCommandBars;

	this->m_pMainFrame->get_CommandBars(&pCommandBars);

	CComBSTR  strTitile;
	strTitile.LoadString(IDR_MAINFRAME);

	CComPtr<saf::ICommandBar>	pResCommandBar;
	pCommandBars->InsertResourceCommandBar(strTitile, IDR_MAINFRAME,
											(LONG_PTR)SafGetInstanceHandle(), 1, &pResCommandBar);

	CComPtr<saf::IMenuBar>	pMenuBar;
	pCommandBars->get_MenuBar(&pMenuBar);
	pMenuBar->AddImageList((LONG_PTR)SafGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME), NULL);
}

void CVisualStudioApp::CreateStatusBar()
{
	CComQIPtr<saf::IStatusBar>	_pStatusBar;

	saf::safStatusBarItemInfo	_safItemInfo[] = {
		{ID_FILE_CLOSE, 200},
		{ID_FILE_NEW, 100}
	};

	m_pMainFrame->get_StatusBar(&_pStatusBar);
	_pStatusBar->SetPanes(_safItemInfo, 2);
}

